Skip to content

Commit

Permalink
feat: modify ACL of advanced settings
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe committed Aug 28, 2023
1 parent e2f277c commit f8b1661
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,33 @@ export class SavedObjectsRepository {
};
}

async deleteACL<T = unknown>(type: string, id: string): Promise<boolean> {
if (!this._allowedTypes.includes(type)) {
throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id);
}

const { statusCode } = await this.client.update<SavedObjectsRawDocSource>(
{
id: this._serializer.generateRawId(undefined, type, id),
index: this.getIndexForType(type),

body: {
script: {
source: "ctx._source.remove('permissions')",
},
},
},
{ ignore: [404] }
);

if (statusCode === 404) {
// see "404s from missing index" above
throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id);
}

return true;
}

/**
* Returns index specified by the given type or the default index
*
Expand Down
55 changes: 51 additions & 4 deletions src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
private coreStart?: CoreStart;
private config$: Observable<ConfigSchema>;
private enabled$: BehaviorSubject<boolean> = new BehaviorSubject(false);
private version: string;

private get isEnabled() {
return this.enabled$.getValue();
Expand Down Expand Up @@ -63,6 +64,7 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get('plugins', 'workspace');
this.config$ = initializerContext.config.create<ConfigSchema>();
this.version = initializerContext.env.packageInfo.version;
}

public async setup(core: CoreSetup) {
Expand Down Expand Up @@ -183,12 +185,21 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
]);
}

private getInternalRepository() {
if (!this.coreStart) {
throw new Error('UI setting client can not be found');
}
const { savedObjects } = this.coreStart as CoreStart;

return savedObjects.createInternalRepository();
}

private async getUISettingClient() {
if (!this.coreStart) {
throw new Error('UI setting client can not be found');
}
const { uiSettings, savedObjects } = this.coreStart as CoreStart;
const internalRepository = savedObjects.createInternalRepository();
const { uiSettings } = this.coreStart as CoreStart;
const internalRepository = this.getInternalRepository();
const savedObjectClient = new SavedObjectsClient(internalRepository);
return uiSettings.asScopedToClient(savedObjectClient);
}
Expand All @@ -206,17 +217,53 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
return workspaceEnabled;
}

public start(core: CoreStart) {
private async setupUISettinsACL() {
const internalRepository = this.getInternalRepository();
const CONFIG_TYPE = 'config';
try {
await internalRepository.get(CONFIG_TYPE, this.version);
} catch (e) {
try {
const uiSettingClient = await this.getUISettingClient();
await uiSettingClient.set(FEATURE_FLAG_KEY_IN_UI_SETTING, false);
} catch (error: unknown) {
if (typeof error === 'string') {
this.logger.error(error);
} else {
this.logger.error(`Something went wrong when setup advanced settings: ${error}`);
}
}
}

if (this.isEnabled) {
const acl = new ACL().addPermission([WorkspacePermissionMode.Read], {
users: ['*'],
});
await internalRepository.update(
CONFIG_TYPE,
this.version,
{},
{
permissions: acl.getPermissions(),
}
);
} else {
await internalRepository.deleteACL(CONFIG_TYPE, this.version);
}
}

public async start(core: CoreStart) {
this.logger.debug('Starting SavedObjects service');

this.coreStart = core;

this.setupWorkspaceFeatureFlag();
await this.setupWorkspaceFeatureFlag();

this.enabled$.subscribe((enabled) => {
if (enabled) {
this.setupWorkspaces();
}
this.setupUISettinsACL();
});

return {
Expand Down

0 comments on commit f8b1661

Please sign in to comment.