Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature flags #686

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/backend/src/CoreModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ const install = async ({ services, app, useapi }) => {

const { BootScriptService } = require('./services/BootScriptService');
services.registerService('boot-script', BootScriptService);

const { FeatureFlagService } = require('./services/FeatureFlagService');
services.registerService('feature-flag', FeatureFlagService);
}

const install_legacy = async ({ services }) => {
Expand Down
1 change: 1 addition & 0 deletions src/backend/src/data/hardcoded-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const hardcoded_user_group_permissions = {
system: {
'ca342a5e-b13d-4dee-9048-58b11a57cc55': {
'service': {},
'feature': {},
},
'b7220104-7905-4985-b996-649fdcdb3c8f': {
'service:hello-world:ii:hello-world': policy_perm('temp.es'),
Expand Down
22 changes: 22 additions & 0 deletions src/backend/src/middleware/featureflag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const APIError = require("../api/APIError");
const { Context } = require("../util/context");

const featureflag = options => async (req, res, next) => {
const { feature } = options;

const context = Context.get();
const services = context.get('services');
const svc_featureFlag = services.get('feature-flag');

if ( ! await svc_featureFlag.check({
actor: req.actor,
}, feature) ) {
const e = APIError.create('forbidden');
e.write(res);
return;
}

next();
};

module.exports = featureflag;
42 changes: 42 additions & 0 deletions src/backend/src/services/FeatureFlagService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { Context } = require("../util/context");
const { whatis } = require("../util/langutil");
const { PermissionUtil } = require("./auth/PermissionService");
const BaseService = require("./BaseService");

/**
* FeatureFlagService is a way to let the client (frontend) know what features
* are enabled or disabled for the current user.
*/
class FeatureFlagService extends BaseService {
async check (...a) {
// allows binding call with multiple options objects;
// the last argument is the permission to check
const { options, value: permission } = (() => {
let value;
const options = {};
for ( const arg of a ) {
if ( whatis(arg) === 'object' ) {
Object.assign(options, arg);
continue;
}
value = arg;
break;
}
return { options, value };
})();



const actor = options.actor ?? Context.get('actor');

const svc_permission = this.services.get('permission');
const reading = await svc_permission.scan(actor, `feature:${permission}`);
const l = PermissionUtil.reading_to_options(reading);
if ( l.length === 0 ) return false;
return true;
}
}

module.exports = {
FeatureFlagService
};
6 changes: 5 additions & 1 deletion src/backend/src/services/ShareService.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
const APIError = require("../api/APIError");
const { get_user } = require("../helpers");
const configurable_auth = require("../middleware/configurable_auth");
const featureflag = require("../middleware/featureflag.js");
const { Context } = require("../util/context");
const { Endpoint } = require("../util/expressutil");
const { whatis } = require("../util/langutil");
Expand Down Expand Up @@ -246,7 +247,10 @@ class ShareService extends BaseService {
Endpoint({
route: '/',
methods: ['POST'],
mw: [configurable_auth()],
mw: [
configurable_auth(),
featureflag({ feature: 'share' }),
],
handler: async (req, res) => {
const actor = Actor.adapt(req.user);
return await share_sequence.call(this, {
Expand Down
8 changes: 5 additions & 3 deletions src/backend/src/services/auth/ACLService.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ class ACLService extends BaseService {
}

// Hard rule: anyone and anything can read /user/public directories
const public_modes = ['read', 'list', 'see'];
if ( public_modes.includes(mode) ) {
if ( await fsNode.isPublic() ) return true;
if ( this.global_config.enable_public_folders ) {
const public_modes = ['read', 'list', 'see'];
if ( public_modes.includes(mode) ) {
if ( await fsNode.isPublic() ) return true;
}
}

// Access tokens only work if the authorizer has permission
Expand Down