1
0
Fork 0
mirror of synced 2024-10-02 10:16:27 +13:00

Fix function request filter

This commit is contained in:
Matej Bačo 2023-08-22 15:16:07 +02:00
parent 6184b6a378
commit eea43e17bd
5 changed files with 34 additions and 22 deletions

View file

@ -409,7 +409,7 @@ return [
],
Exception::FUNCTION_ENTRYPOINT_MISSING => [
'name' => Exception::FUNCTION_RUNTIME_UNSUPPORTED,
'description' => 'Entrypoint missing. Specify it in function settings.',
'description' => 'Function entrypoint is not configured. Please specify it in function settings or when making deployment.',
'code' => 404,
],

View file

@ -134,14 +134,14 @@ App::post('/v1/functions')
->label('sdk.response.model', Response::MODEL_FUNCTION)
->param('functionId', '', new CustomId(), 'Function ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.')
->param('name', '', new Text(128), 'Function name. Max length: 128 chars.')
->param('runtime', '', new WhiteList(array_keys(Config::getParam('runtimes')), true), 'Execution runtime.')
->param('execute', [], new Roles(APP_LIMIT_ARRAY_PARAMS_SIZE), 'An array of strings with execution roles. By default no user is granted with any execute permissions. [learn more about permissions](https://appwrite.io/docs/permissions). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' roles are allowed, each 64 characters long.', true)
->param('runtime', '', new WhiteList(array_keys(Config::getParam('runtimes')), true), 'Execution runtime.')
->param('events', [], new ArrayList(new ValidatorEvent(), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Events list. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' events are allowed.', true)
->param('schedule', '', new Cron(), 'Schedule CRON syntax.', true)
->param('timeout', 15, new Range(1, (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900)), 'Function maximum execution time in seconds.', true)
->param('enabled', true, new Boolean(), 'Is function enabled?', true)
->param('logging', true, new Boolean(), 'Do executions get logged?', true)
->param('entrypoint', '', new Text(1028), 'Entrypoint File.')
->param('entrypoint', '', new Text(1028, 0), 'Entrypoint File.', true)
->param('commands', '', new Text(8192, 0), 'Build Commands.', true)
->param('installationId', '', new Text(128, 0), 'Appwrite Installation ID for vcs deployment.', true)
->param('providerRepositoryId', '', new Text(128, 0), 'Repository ID of the repo linked to the function', true)
@ -228,7 +228,7 @@ App::post('/v1/functions')
'active' => false,
]))
);
$function->setAttribute('scheduleId', $schedule->getId());
$function->setAttribute('scheduleInternalId', $schedule->getInternalId());
@ -607,14 +607,14 @@ App::put('/v1/functions/:functionId')
->label('sdk.response.model', Response::MODEL_FUNCTION)
->param('functionId', '', new UID(), 'Function ID.')
->param('name', '', new Text(128), 'Function name. Max length: 128 chars.')
->param('runtime', '', new WhiteList(array_keys(Config::getParam('runtimes')), true), 'Execution runtime.')
->param('execute', [], new Roles(APP_LIMIT_ARRAY_PARAMS_SIZE), 'An array of strings with execution roles. By default no user is granted with any execute permissions. [learn more about permissions](https://appwrite.io/docs/permissions). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' roles are allowed, each 64 characters long.', true)
->param('runtime', '', new WhiteList(array_keys(Config::getParam('runtimes')), true), 'Execution runtime.')
->param('events', [], new ArrayList(new ValidatorEvent(), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Events list. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' events are allowed.', true)
->param('schedule', '', new Cron(), 'Schedule CRON syntax.', true)
->param('timeout', 15, new Range(1, (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900)), 'Maximum execution time in seconds.', true)
->param('enabled', true, new Boolean(), 'Is function enabled?', true)
->param('logging', true, new Boolean(), 'Do executions get logged?', true)
->param('entrypoint', '', new Text(1028), 'Entrypoint File.')
->param('entrypoint', '', new Text(1028, 0), 'Entrypoint File.', true)
->param('commands', '', new Text(8192, 0), 'Build Commands.', true)
->param('installationId', '', new Text(128, 0), 'Appwrite Installation ID for vcs deployment.', true)
->param('providerRepositoryId', '', new Text(128, 0), 'Repository ID of the repo linked to the function', true)
@ -656,6 +656,10 @@ App::put('/v1/functions/:functionId')
$repositoryId = $function->getAttribute('repositoryId', '');
$repositoryInternalId = $function->getAttribute('repositoryInternalId', '');
if (empty($entrypoint)) {
$entrypoint = $function->getAttribute('entrypoint', '');
}
$isConnected = !empty($function->getAttribute('providerRepositoryId', ''));
// Git disconnect logic
@ -803,7 +807,7 @@ App::get('/v1/functions/:functionId/deployments/:deploymentId/download')
->setContentType('application/gzip')
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache
->addHeader('X-Peak', \memory_get_peak_usage())
->addHeader('Content-Disposition', 'attachment; filename="'.$deploymentId.'.tar.gz"')
->addHeader('Content-Disposition', 'attachment; filename="' . $deploymentId . '.tar.gz"')
;
$size = $deviceFunctions->getFileSize($path);

View file

@ -45,7 +45,7 @@ App::post('/v1/proxy/rules')
if ($domain === $mainDomain || $domain === 'localhost' || $domain === APP_HOSTNAME_INTERNAL) {
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed for security reasons.');
}
$document = $dbForConsole->findOne('rules', [
Query::equal('domain', [$domain]),
]);

View file

@ -86,6 +86,10 @@ class BuildsV1 extends Worker
throw new Exception('Deployment not found', 404);
}
if (empty($deployment->getAttribute('entrypoint', ''))) {
throw new Exception('Function entrypoint is not configured. Please specify it in function settings or when making deployment.', 500);
}
$runtimes = Config::getParam('runtimes', []);
$key = $function->getAttribute('runtime');
$runtime = isset($runtimes[$key]) ? $runtimes[$key] : null;

View file

@ -6,33 +6,37 @@ use Appwrite\Utopia\Request\Filter;
class V16 extends Filter
{
// Convert 1.0 params to 1.4
// Convert 1.3 params to 1.4
public function parse(array $content, string $model): array
{
switch ($model) {
case 'functions.create':
// TODO: How to handle this?
$content['entrypoint'] = ' ';
$content['commands'] = $this->getCommands($content['runtime']);
break;
case 'functions.update':
// TODO: How to handle this?
$content['runtime'] = ' ';
$content['commands'] = $this->getCommands($content['runtime']);
break;
case 'functions.createExecution':
$content['body'] = $content['data'];
unset($content['data']);
break;
case 'projects.createDomain':
case 'projects.listDomains':
case 'projects.getDomain':
case 'projects.updateDomainVerification':
case 'projects.deleteDomain':
// These endpoints were deleted and we're accepting
// the breaking change since the endpoint was only
// used internally.
break;
}
return $content;
}
private function getCommands(string $runtime): string
{
if (\str_starts_with($runtime, 'node')) {
return 'npm install';
} elseif (\str_starts_with($runtime, 'python')) {
return 'pip install --no-cache-dir -r requirements.txt';
} elseif (\str_starts_with($runtime, 'dart')) {
return 'dart pub get';
} elseif (\str_starts_with($runtime, 'php')) {
return 'composer update --no-interaction --ignore-platform-reqs --optimize-autoloader --prefer-dist --no-dev';
}
return '';
}
}