ClientSettings api

This commit is contained in:
gtoriadotnet 2022-03-24 00:05:07 -04:00
parent 6ec79d2f36
commit e67e83c1b6
14 changed files with 306 additions and 30 deletions

View File

@ -0,0 +1,15 @@
<?php
/*
Graphictoria 2022
JSON Pretty Printer
*/
namespace App\Helpers;
class COMHelper
{
public static function isCOM() {
return (php_sapi_name() === 'cli');
}
}

View File

@ -0,0 +1,35 @@
<?php
/*
Graphictoria 2022
JSON Pretty Printer
*/
namespace App\Helpers;
use App\Helpers\COMHelper;
use App\Models\WebsiteConfiguration;
class GridHelper
{
public static function isIpWhitelisted($request) {
$ip = $request->ip();
$whitelistedIps = explode(';', WebsiteConfiguration::where('name', 'WhitelistedIPs')->first()->value);
return in_array($ip, $whitelistedIps);
}
public static function isAccessKeyValid($request) {
$accessKey = WebsiteConfiguration::where('name', 'ComputeServiceAccessKey')->first()->value;
return ($request->header('AccessKey') == $accessKey);
}
public static function hasAllAccess($request) {
if(COMHelper::isCOM()) return true;
if(GridHelper::isIpWhitelisted($request) && GridHelper::isAccessKeyValid($request)) return true;
return false;
}
}

21
web/app/Helpers/JSON.php Normal file
View File

@ -0,0 +1,21 @@
<?php
/*
Graphictoria 2022
JSON Pretty Printer
*/
namespace App\Helpers;
class JSON
{
public static function EncodeResponse($array) {
$json = json_encode(
$array,
JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS
);
return response($json)
->header('Content-Type', 'application/json');
}
}

View File

@ -0,0 +1,106 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use App\Helpers\JSON;
use App\Helpers\GridHelper;
use App\Models\FFlag;
use App\Models\Fbucket;
class AppSettings extends Controller
{
/**
* A list of flag prefixes
*
* @var array
*/
protected $prefixes = [
'Unscoped' => '',
'Fast' => 'F',
'Dynamic' => 'DF',
'Synchronised' => 'SF'
];
/**
* A list of flag types
*
* @var array
*/
protected $types = [
'Log' => 'Log',
'Int' => 'Int',
'String' => 'String',
'Boolean' => 'Flag'
];
/**
* Returns a JSON array with the error code and message.
*
* @return Response
*/
private function error($data, $code = 400)
{
return response(['errors' => [$data]], 400);
}
/**
* Returns a JSON array of settings for the specified bucket.
*
* @param \Illuminate\Http\Request $request
* @param string $bucketName
* @return Response
*/
public function getBucket(Request $request, $bucketName)
{
$primaryBucket = Fbucket::where('name', $bucketName);
if($primaryBucket->exists()) {
$primaryBucket = $primaryBucket->first();
$bucketIds = [ $primaryBucket->id ];
$bucketIds = array_merge($bucketIds, json_decode($primaryBucket->inheritedGroupIds));
if($primaryBucket->protected == 1 && !GridHelper::hasAllAccess($request)) {
return $this->error([
'code' => 2,
'message' => 'You do not have access to this bucket.'
], 401);
}
/* */
$flags = [];
foreach($bucketIds as $bucket) {
$fflags = FFlag::where('bucketId', $bucket)->get();
foreach($fflags as $flag) {
$prefix = $this->prefixes[$flag->type];
$dataType = $this->types[$flag->dataType];
$name = '';
if($prefix != 'Unscoped') {
$name = ($prefix . $dataType);
}
$name .= $flag->name;
$flags[$name] = $flag->value;
}
}
return JSON::EncodeResponse($flags);
} else {
return $this->error([
'code' => 1,
'message' => 'The requested bucket does not exist.'
]);
}
}
}

View File

@ -7,5 +7,19 @@ use Illuminate\Database\Eloquent\Model;
class FFlag extends Model
{
/**
* The database connection that should be used by the migration.
*
* @var string
*/
protected $connection = 'mysql-fflag';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'fflags';
use HasFactory;
}

View File

@ -1,11 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Fastgroup extends Model
{
use HasFactory;
}

View File

@ -0,0 +1,19 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/* FFlag bucket */
class Fbucket extends Model
{
/**
* The database connection that should be used by the migration.
*
* @var string
*/
protected $connection = 'mysql-fflag';
use HasFactory;
}

View File

@ -45,6 +45,11 @@ class RouteServiceProvider extends ServiceProvider
->namespace($this->namespace)
->group(base_path('routes/apis.php'));
Route::domain('clientsettings.api.' . env('APP_URL'))
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/appsettings.php'));
Route::domain('impulse.' . env('APP_URL'))
->middleware('admin')
->namespace($this->namespace)

View File

@ -15,7 +15,7 @@ return [
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
'default' => env('DB_CONNECTION', 'mysql-primary'),
/*
|--------------------------------------------------------------------------
@ -38,17 +38,37 @@ return [
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'database' => env('DB_PRIMARY_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'mysql' => [
'mysql-primary' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'database' => env('DB_PRIMARY_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql-fflag' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_FFLAG_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
@ -68,7 +88,7 @@ return [
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'database' => env('DB_PRIMARY_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
@ -83,7 +103,7 @@ return [
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'database' => env('DB_PRIMARY_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',

View File

@ -6,6 +6,13 @@ use Illuminate\Support\Facades\Schema;
class CreateFflagsTable extends Migration
{
/**
* The database connection that should be used by the migration.
*
* @var string
*/
protected $connection = 'mysql-fflag';
/**
* Run the migrations.
*
@ -19,7 +26,7 @@ class CreateFflagsTable extends Migration
$table->string('value');
$table->enum('dataType', ['Log', 'Int', 'String', 'Boolean']);
$table->enum('type', ['Unscoped', 'Fast', 'Dynamic', 'Synchronised']);
$table->bigInteger('groupId');
$table->bigInteger('bucketId');
$table->timestamps();
});
}

View File

@ -4,8 +4,15 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFastgroupsTable extends Migration
class CreateFbucketsTable extends Migration
{
/**
* The database connection that should be used by the migration.
*
* @var string
*/
protected $connection = 'mysql-fflag';
/**
* Run the migrations.
*
@ -13,7 +20,7 @@ class CreateFastgroupsTable extends Migration
*/
public function up()
{
Schema::create('fastgroups', function (Blueprint $table) {
Schema::create('fbuckets', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('protected')->default(false);
@ -29,6 +36,6 @@ class CreateFastgroupsTable extends Migration
*/
public function down()
{
Schema::dropIfExists('fastgroups');
Schema::dropIfExists('fbuckets');
}
}

View File

@ -5,7 +5,7 @@ namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\FFlag;
use App\Models\Fastgroup;
use App\Models\Fbucket;
class FFlagSeeder extends Seeder
{
@ -16,41 +16,41 @@ class FFlagSeeder extends Seeder
*/
public function run()
{
$appSettingsCommon = Fastgroup::create([
$appSettingsCommon = Fbucket::create([
'name' => 'AppSettingsCommon',
'protected' => true
]);
$clientAppSettings = Fastgroup::create([
$clientAppSettings = Fbucket::create([
'name' => 'ClientAppSettings',
'inheritedGroupIds' => json_encode([ $appSettingsCommon->id ])
]);
$cloudAppSettings = Fastgroup::create([
$cloudAppSettings = Fbucket::create([
'name' => 'CloudCompute',
'protected' => true,
'inheritedGroupIds' => json_encode([ $appSettingsCommon->id ])
]);
$clientSharedSettings = Fastgroup::create([
$clientSharedSettings = Fbucket::create([
'name' => 'ClientSharedSettings'
]);
$arbiterAppSettings = Fastgroup::create([
$arbiterAppSettings = Fbucket::create([
'name' => 'Arbiter'
]);
$bootstrapperCommon = Fastgroup::create([
$bootstrapperCommon = Fbucket::create([
'name' => 'BootstrapperCommon',
'protected' => true
]);
$windowsBootstrapperSettings = Fastgroup::create([
$windowsBootstrapperSettings = Fbucket::create([
'name' => 'WindowsBootstrapperSettings',
'inheritedGroupIds' => json_encode([ $bootstrapperCommon->id ])
]);
$windowsStudioBootstrapperSettings = Fastgroup::create([
$windowsStudioBootstrapperSettings = Fbucket::create([
'name' => 'WindowsStudioBootstrapperSettings',
'inheritedGroupIds' => json_encode([ $bootstrapperCommon->id ])
]);

View File

@ -23,5 +23,15 @@ class WebConfigurationSeeder extends Seeder
'password' => '@bs0lut3lyM@55!v3P@55w0rd'
])
]); // please please please please please please please change the default password
WebsiteConfiguration::create([
'name' => 'ComputeServiceAccessKey',
'value' => '92a6ac6b-7167-49b1-9ccd-079820ac892b'
]); // change this as well
WebsiteConfiguration::create([
'name' => 'WhitelistedIPs',
'value' => '127.0.0.1'
]);
}
}

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AppSettings;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::get('/', function(){
return 'API OK';
});
Route::get('/Setting/QuietGet/{Bucket}', 'AppSettings@getBucket');
Route::fallback(function(){
return response('{"errors":[{"code":404,"message":"NotFound"}]}', 404)
->header('Cache-Control', 'private')
->header('Content-Type', 'application/json; charset=utf-8');
});