Skip to content

Commit

Permalink
Merge pull request #354 from matematikk-mooc/aj/DIT-226
Browse files Browse the repository at this point in the history
DIT-226: Fix for CI/CD pipeline
  • Loading branch information
ajxudir authored Aug 28, 2024
2 parents 0b309aa + 649b470 commit 3e971b7
Show file tree
Hide file tree
Showing 13 changed files with 438 additions and 316 deletions.
9 changes: 8 additions & 1 deletion .docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ RUN apt-get update && apt-get install -y \
libxml2-dev \
libzip-dev \
zlib1g-dev \
openssl \
openssh-client \
openssl && \
libssl-dev \
ca-certificates && \
update-ca-certificates && \
apt-get clean && rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-install gd \
Expand All @@ -42,6 +45,10 @@ RUN mkdir -p $NVM_DIR \
# 2. Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# 3. Install gomplate
RUN curl -L https://github.com/hairyhenderson/gomplate/releases/download/v3.11.3/gomplate_linux-amd64-slim -o /usr/local/bin/gomplate && \
chmod +x /usr/local/bin/gomplate

##############################################################
# Startup
##############################################################
Expand Down
75 changes: 75 additions & 0 deletions .docker/lti_templates/role_group.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"title": "kpas-local-{{ getenv "CANVAS_ACCESS_KEY_NAME" }}-001",
"description": "kpas-local-{{ getenv "CANVAS_ACCESS_KEY_NAME" }}-001",
"target_link_uri": "{{ getenv "APP_URL" }}/launch?config_directory=configs",
"oidc_initiation_url": "{{ getenv "APP_URL" }}/lti3?config_directory=configs",
"extensions": [
{
"platform": "canvas.instructure.com",
"privacy_level": "public",
"settings": {
"platform": "canvas.instructure.com",
"placements": [
{
"placement": "course_navigation",
"message_type": "LtiResourceLinkRequest",
"icon_url": "https://icons.iconarchive.com/icons/papirus-team/papirus-places/64/folder-blue-linux-icon.png",
"target_link_uri": "{{ getenv "APP_URL" }}/launch?config_directory=configs",
"text": "kpas-local-{{ getenv "CANVAS_ACCESS_KEY_NAME" }}-001",
"selection_width": 800,
"selection_height": 800
},
{
"placement": "editor_button",
"message_type": "LtiDeepLinkingRequest",
"icon_url": "https://icons.iconarchive.com/icons/papirus-team/papirus-places/64/folder-blue-linux-icon.png",
"target_link_uri": "{{ getenv "APP_URL" }}/launch?config_directory=configs",
"text": "kpas-local-{{ getenv "CANVAS_ACCESS_KEY_NAME" }}-001",
"selection_height": 800,
"selection_width": 800
}
]
}
}
],
"custom_fields": {
"FACULTY_OPTION_1": "Matematikk 1-7",
"FACULTY_OPTION_2": "Matematikk 8-10",
"FACULTY_OPTION_3": "Matematikk vgs",
"FACULTY_OPTION_4": "Kunst og håndverk 1-7",
"FACULTY_OPTION_5": "Kunst og håndverk 8-10",
"FACULTY_OPTION_6": "Kunst og håndverk vgs",
"FACULTY_OPTION_7": "Naturfag 1-7",
"FACULTY_OPTION_8": "Naturfag 8-10",
"custom_canvas_roles": "$Canvas.membership.roles",
"county_category_name": "Fylke",
"school_category_name": "Skole",
"custom_canvas_user_id": "$Canvas.user.id",
"community_category_name": "Kommune",
"custom_canvas_course_id": "$Canvas.course.id",
"custom_canvas_account_id": "$Canvas.account.id",
"custom_canvas_course_name": "$Canvas.course.name",
"county_faculty_category_name": "Faggruppe kommune",
"community_faculty_category_name": "Faggruppe fylke",
"county_principals_category_name": "Leder/eier (fylke)",
"custom_canvas_user_display_name": "$Person.name.display",
"community_principals_category_name": "Leder/eier (kommune)"
},
"public_jwk": {{ getenv "PUBLIC_JWK_JSON" }},
"scopes": [
"https://purl.imsglobal.org/spec/lti-ags/scope/lineitem",
"https://purl.imsglobal.org/spec/lti-ags/scope/lineitem.readonly",
"https://purl.imsglobal.org/spec/lti-ags/scope/result.readonly",
"https://purl.imsglobal.org/spec/lti-ags/scope/score",
"https://purl.imsglobal.org/spec/lti-nrps/scope/contextmembership.readonly",
"https://canvas.instructure.com/lti/public_jwk/scope/update",
"https://canvas.instructure.com/lti/account_lookup/scope/show",
"https://canvas.instructure.com/lti/data_services/scope/create",
"https://canvas.instructure.com/lti/data_services/scope/show",
"https://canvas.instructure.com/lti/data_services/scope/update",
"https://canvas.instructure.com/lti/data_services/scope/destroy",
"https://canvas.instructure.com/lti/data_services/scope/list",
"https://canvas.instructure.com/lti/data_services/scope/list_event_types",
"https://canvas.instructure.com/lti/feature_flags/scope/show"
]
}
3 changes: 2 additions & 1 deletion .docker/php.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
allow_url_fopen=On
memory_limit=-1
display_errors=Off
display_startup_errors=Off
error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT
Expand All @@ -6,4 +8,3 @@ log_errors=On
expose_php=Off
[Date]
date.timezone="UTC"

64 changes: 44 additions & 20 deletions .docker/startup.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash

echo -e "\n\n\n[1/7] Copy and import .env variables to the current shell"
export PATH=$PATH:/usr/local/bin

echo -e "\n\n\n[1/8] Copy and import .env variables to the current shell"
echo -e "##############################################################\n"
ENV_FILE=".env"
if [ ! -f "$ENV_FILE" ]; then
Expand All @@ -15,76 +17,98 @@ while IFS='=' read -r key value; do

if [[ -z "${!key}" ]]; then
export "$key=$value"
echo "- IMPORTED: $key"
echo " - IMPORTED: $key"
else
echo "- ERROR: $key"
echo " - ERROR: $key"
fi
fi
done < "$ENV_FILE"
set +o allexport

echo -e "\n\n\n[2/7] Install NodeJS using NVM"
echo -e "\n\n\n[2/8] Install NodeJS using NVM"
echo -e "##############################################################\n"
source /usr/local/nvm/nvm.sh
nvm install

echo -e "\n\n\n[3/7] Install NPM packages"
echo -e "\n\n\n[3/8] Install NPM packages"
echo -e "##############################################################\n"
npm install

echo -e "\n\n\n[4/7] Install composer packages"
echo -e "\n\n\n[4/8] Install composer packages"
echo -e "##############################################################\n"
composer install --no-dev --prefer-dist --no-interaction --optimize-autoloader

echo -e "\n\n\n[5/7] Setup JWT key pair & config"
echo -e "\n\n\n[5/8] Setup JWT key pair & config"
echo -e "##############################################################\n"
jwtDir="app/Ltiv3/jwt_key_kpas"
jwtKey="$jwtDir/jwtRS256.key"
if [ -f "$jwtKey" ]; then
echo -e " - File $jwtKey already exists, delete this file and restart if you want to create a new JWT key pair"
echo -e " - File $jwtKey already exists, delete this file and restart if you want to create a new JWT key pair"
else
echo -e " - Creating new directory for JWT $jwtDir..."
echo -e " - Creating new directory for JWT $jwtDir..."
mkdir -p "$jwtDir"

echo -e " - Creating new RSA private key $jwtDir/jwtRS256.key..."
echo -e " - Creating new RSA private key $jwtDir/jwtRS256.key..."
ssh-keygen -t rsa -b 4096 -m PEM -f $jwtDir/jwtRS256.key -N ""

echo -e " - Creating new RSA public key $jwtDir/jwtRS256.key.pub..."
echo -e " - Creating new RSA public key $jwtDir/jwtRS256.key.pub..."
openssl rsa -in $jwtDir/jwtRS256.key -pubout -outform PEM -out $jwtDir/jwtRS256.key.pub

echo -e " - Installing pem-jwk CLI tool from NPM for JWK generation..."
echo -e " - Installing pem-jwk CLI tool from NPM for JWK generation..."
npm install -g pem-jwk

echo -e " - Creating new JWT key pair $jwtDir/jwtRS256.jwk..."
echo -e " - Creating new JWT key pair $jwtDir/jwtRS256.jwk..."
b64_header=$(echo -n '{"kid":"2","alg":"RS256"}' | openssl base64 -e -A | tr '+/' '-_' | tr -d '=')
jwk=$(pem-jwk $jwtDir/jwtRS256.key.pub | jq '. + {"kid":"2"}')
jwk=$(pem-jwk $jwtDir/jwtRS256.key.pub | jq '. + {"kid":"2","alg":"RS256","use":"enc"}')
echo $jwk | jq . > $jwtDir/jwtRS256.json
fi

jwtConfigsDir="database/configs"
if [ -d "$jwtConfigsDir" ]; then
echo -e " - Directory $jwtConfigsDir already exists, delete this folder and restart if you want to create a new config template"
echo -e " - Directory $jwtConfigsDir already exists, delete this folder and restart if you want to create a new config template"
else
echo -e " - Creating new directory for JWT configs $jwtConfigsDir..."
echo -e " - Creating new directory for JWT configs $jwtConfigsDir..."
mkdir -p "$jwtConfigsDir"

echo -e " - Creating empty JWT config template $jwtConfigsDir/config_platform.json..."
echo -e " - Creating empty JWT config template $jwtConfigsDir/config_platform.json..."
cat <<EOF > $jwtConfigsDir/config_platform.json
{
"https://canvas.test.instructure.com": {
"client_id": "",
"auth_login_url": "https://sso.test.canvaslms.com/api/lti/authorize_redirect",
"auth_token_url": "https://sso.test.canvaslms.com/login/oauth2/token",
"key_set_url": "https://sso.test.canvaslms.com/api/lti/security/jwks",
"private_key_file": "$dir/jwtRS256.key",
"private_key_file": "/jwt_key_kpas/jwtRS256.key",
"kid": "2",
"deployment": []
}
}
EOF
fi

echo -e "\n\n\n[6/7] Run artisan commands"
echo -e "\n\n\n[6/8] Setup LTI registration templates"
echo -e "##############################################################\n"
jwtTemplatesDir=".docker/lti_templates"
jwtTemplatesOutputDir="database/templates"
export PUBLIC_JWK_JSON=$(cat "$jwtDir/jwtRS256.json")

mkdir -p "$jwtTemplatesOutputDir"
echo -e " LTI templates output directory: $jwtTemplatesOutputDir"

for template in "$jwtTemplatesDir"/*.tpl; do
filename=$(basename "$template" .tpl)
outputFile="$jwtTemplatesOutputDir/$filename.json"
outputExec="Failed to generate JWT template"

gomplate -f "$template" -o "$outputFile"
if [ $? -eq 0 ]; then
outputExec="Successfully generated JWT template"
fi

echo -e " - $filename: $outputExec"
done

echo -e "\n\n\n[7/8] Run artisan commands"
echo -e "##############################################################\n"
php artisan cache:clear
php artisan route:clear
Expand All @@ -94,6 +118,6 @@ php artisan route:cache
php artisan view:cache
php artisan migrate --force

echo -e "\n\n\n[7/7] Start Supervisor"
echo -e "\n\n\n[8/8] Start Supervisor"
echo -e "##############################################################\n"
/usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20.15.0
v20.17.0
23 changes: 13 additions & 10 deletions app/Services/CanvasGraphQLService.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,24 @@ function modulesConnection($course_id)
try {
$url = str_replace('/v1', '', $this->domain) . '/graphql';
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $this->accessKey,
'Content-Type: application/json'
);
$options = array(
'http' => array(
'method' => 'POST',
'header' => implode("\r\n", $headers),
'content' => json_encode(array('query' => $queryData, 'variables' => array('courseId' => $course_id)))
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$postData = json_encode(array('query' => $queryData, 'variables' => array('courseId' => $course_id)));

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
if ($result === false) {
curl_close($ch);
throw new Exception('Error fetching data');
}

curl_close($ch);
$response = json_decode($result, true);
return $response;
} catch (Exception $error) {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"require": {
"php": "^8.1",
"php": "^8.3",
"ext-json": "*",
"bugsnag/bugsnag-laravel": "^2.28",
"doctrine/dbal": "^3.8",
Expand Down
Loading

0 comments on commit 3e971b7

Please sign in to comment.