Skip to content

Commit

Permalink
Improve management of ssh keys by using them directly from Playwright…
Browse files Browse the repository at this point in the history
… directory
  • Loading branch information
muradium committed Dec 14, 2024
1 parent 3fba5ac commit 13ad227
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ data-exports/
######################
/src/test/playwright/test-reports/
/src/test/playwright/test-results/*
/src/test/playwright/ssh-keys/known_hosts

#################################
# Files generated by prebuild.mjs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import pythonAllSuccessful from '../../../fixtures/exercise/programming/python/a
import { BASE_API, ExerciseCommit, ExerciseMode, ProgrammingLanguage } from '../../../support/constants';
import { test } from '../../../support/fixtures';
import { BrowserContext, Page, expect } from '@playwright/test';
import { gitClient } from '../../../support/pageobjects/exercises/programming/GitClient';
import { SSH_KEYS_PATH, SSH_KEY_NAMES, SshEncryptionAlgorithm, gitClient } from '../../../support/pageobjects/exercises/programming/GitClient';
import * as fs from 'fs/promises';
import path from 'path';
import { SimpleGit } from 'simple-git';
Expand All @@ -20,7 +20,6 @@ import { UserCredentials, admin, instructor, studentFour, studentOne, studentTwo
import { Team } from 'app/entities/team.model';
import { GitCloneMethod, ProgrammingExerciseOverviewPage } from '../../../support/pageobjects/exercises/programming/ProgrammingExerciseOverviewPage';
import { Participation } from 'app/entities/participation/participation.model';
import { SSH_KEY_NAMES, SshEncryptionAlgorithm } from '../../../init/global-setup';

test.describe('Programming exercise participation', { tag: '@sequential' }, () => {
let course: Course;
Expand Down Expand Up @@ -301,8 +300,7 @@ async function makeGitExerciseSubmission(
async function setupSSHCredentials(context: BrowserContext, sshAlgorithm: SshEncryptionAlgorithm) {
console.log(`Setting up SSH credentials with key ${SSH_KEY_NAMES[sshAlgorithm]}`);
const page = await context.newPage();
const playwrightRoot = process.cwd();
const sshKeyPath = path.join(playwrightRoot, 'ssh-keys', `${SSH_KEY_NAMES[sshAlgorithm]}.pub`);
const sshKeyPath = path.join(SSH_KEYS_PATH, `${SSH_KEY_NAMES[sshAlgorithm]}.pub`);
const sshKey = await fs.readFile(sshKeyPath, 'utf8');
await page.goto('user-settings/ssh');
await page.getByTestId('addNewSshKeyButton').click();
Expand Down
41 changes: 6 additions & 35 deletions src/test/playwright/init/global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,18 @@
import fs from 'fs';
import path from 'path';
import os from 'os';

export enum SshEncryptionAlgorithm {
rsa = 'RSA',
ed25519 = 'ED25519',
}

export const SSH_KEY_NAMES = {
[SshEncryptionAlgorithm.rsa]: 'artemis_playwright_rsa',
[SshEncryptionAlgorithm.ed25519]: 'artemis_playwright_ed25519',
};
import { SSH_KEYS_PATH, SSH_KEY_NAMES } from '../support/pageobjects/exercises/programming/GitClient';

async function globalSetup() {
console.log('Running global setup...');

const sshDir = path.join(os.homedir(), '.ssh');
if (!fs.existsSync(sshDir)) {
fs.mkdirSync(sshDir, { recursive: true });
}

// Set correct permissions to the SSH keys
try {
for (const keyName of Object.values(SSH_KEY_NAMES)) {
const sourceKey = path.join(process.cwd(), 'ssh-keys', keyName);
const sourceKeyPub = path.join(process.cwd(), 'ssh-keys', `${keyName}.pub`);
const destKey = path.join(sshDir, keyName);
const destKeyPub = path.join(sshDir, `${keyName}.pub`);

if (!fs.existsSync(destKey)) {
fs.copyFileSync(sourceKey, destKey);
fs.chmodSync(destKey, 0o600);
console.log(`Private SSH key ${keyName} copied.`);
} else {
console.log(`Private SSH key ${keyName} already exists, skipping.`);
}
const privateKeyPath = path.join(SSH_KEYS_PATH, keyName);
const publicKeyPath = `${privateKeyPath}.pub`;

if (!fs.existsSync(destKeyPub)) {
fs.copyFileSync(sourceKeyPub, destKeyPub);
fs.chmodSync(destKeyPub, 0o644);
console.log(`Public SSH key ${keyName} copied.`);
} else {
console.log(`Public SSH key ${keyName} already exists, skipping.`);
}
fs.chmodSync(privateKeyPath, 0o600);
fs.chmodSync(publicKeyPath, 0o644);
}
} catch (error) {
console.error('Error during SSH key setup:', error);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { simpleGit } from 'simple-git';
import * as fs from 'fs';
import path from 'path';

class GitClient {
async cloneRepo(url: string, repoName: string, sshKeyName?: string) {
const git = simpleGit();
const repoPath = `./${process.env.EXERCISE_REPO_DIRECTORY}/${repoName}`;
const gitSshCommand = sshKeyName ? `ssh -i ~/.ssh/${sshKeyName} -o StrictHostKeyChecking=no` : undefined;
let gitSshCommand;

if (gitSshCommand) {
if (sshKeyName) {
const privateKeyPath = path.join(SSH_KEYS_PATH, sshKeyName);
const knownHostsPath = path.join(SSH_KEYS_PATH, 'known_hosts');
gitSshCommand = `ssh -i ${privateKeyPath} -o UserKnownHostsFile=${knownHostsPath} -o StrictHostKeyChecking=no`;
git.env({ GIT_SSH_COMMAND: gitSshCommand });
}

Expand All @@ -27,3 +31,15 @@ class GitClient {
}

export const gitClient = new GitClient();

export enum SshEncryptionAlgorithm {
rsa = 'RSA',
ed25519 = 'ED25519',
}

export const SSH_KEY_NAMES = {
[SshEncryptionAlgorithm.rsa]: 'artemis_playwright_rsa',
[SshEncryptionAlgorithm.ed25519]: 'artemis_playwright_ed25519',
};

export const SSH_KEYS_PATH = path.join(process.cwd(), 'ssh-keys');

0 comments on commit 13ad227

Please sign in to comment.