Added Integration name extraction based on API Key data

refs https://github.com/TryGhost/Toolbox/issues/292

- This information is needed for the emails used to notify the instance admins about version mismatch.
- The identification information is different for Content and Admin keys because of their nature. Content API keys send the unique "secret" value in plaintext, Admin API keys send key id (kid) as a part of token payload and that's the easiest accessible information available without a need to query the db in another parts of the system.
This commit is contained in:
Naz 2022-05-10 16:26:52 +08:00
parent fbd7e206dc
commit baef69f968
2 changed files with 86 additions and 1 deletions

View File

@ -6,10 +6,12 @@ class VersionNotificationsDataService {
/** /**
* @param {Object} options * @param {Object} options
* @param {Object} options.UserModel - ghost user model * @param {Object} options.UserModel - ghost user model
* @param {Object} options.ApiKeyModel - ghost api key model
* @param {Object} options.settingsService - ghost settings service * @param {Object} options.settingsService - ghost settings service
*/ */
constructor({UserModel, settingsService}) { constructor({UserModel, ApiKeyModel, settingsService}) {
this.UserModel = UserModel; this.UserModel = UserModel;
this.ApiKeyModel = ApiKeyModel;
this.settingsService = settingsService; this.settingsService = settingsService;
} }
@ -49,6 +51,26 @@ class VersionNotificationsDataService {
return adminEmails; return adminEmails;
} }
/**
*
* @param {String} key - api key identification value, it's "secret" in case of Content API key and "id" for Admin API
* @param {String} type - one of "content" or "admin" values
* @returns
*/
async getIntegrationName(key, type) {
let queryOptions = null;
if (type === 'content') {
queryOptions = {secret: key};
} else if (type === 'admin') {
queryOptions = {id: key};
}
const apiKey = await this.ApiKeyModel.findOne(queryOptions, {withRelated: ['integration']});
return apiKey.relations.integration.get('name');
}
} }
module.exports = VersionNotificationsDataService; module.exports = VersionNotificationsDataService;

View File

@ -24,6 +24,7 @@ describe('Version Notification Data Service', function () {
const versionNotificationsDataService = new VersionNotificationsDataService({ const versionNotificationsDataService = new VersionNotificationsDataService({
UserModel: {}, UserModel: {},
ApiKeyModel: {},
settingsService settingsService
}); });
@ -52,6 +53,7 @@ describe('Version Notification Data Service', function () {
const versionNotificationsDataService = new VersionNotificationsDataService({ const versionNotificationsDataService = new VersionNotificationsDataService({
UserModel: {}, UserModel: {},
ApiKeyModel: {},
settingsService settingsService
}); });
@ -110,6 +112,7 @@ describe('Version Notification Data Service', function () {
const versionNotificationsDataService = new VersionNotificationsDataService({ const versionNotificationsDataService = new VersionNotificationsDataService({
UserModel, UserModel,
ApiKeyModel: {},
settingsService: {} settingsService: {}
}); });
@ -119,4 +122,64 @@ describe('Version Notification Data Service', function () {
assert.deepEqual(emails, ['simon@example.com', 'bob@example.com']); assert.deepEqual(emails, ['simon@example.com', 'bob@example.com']);
}); });
}); });
describe('getIntegrationName', function () {
it('Queries for Content API key when such is provided', async function () {
const ApiKeyModel = {
findOne: sinon
.stub()
.withArgs({
secret: 'super_secret'
}, {
withRelated: ['integration']
})
.resolves({
relations: {
integration: {
get: () => 'live fast die young'
}
}
})
};
const versionNotificationsDataService = new VersionNotificationsDataService({
UserModel: {},
ApiKeyModel,
settingsService: {}
});
const integrationName = await versionNotificationsDataService.getIntegrationName('super_secret', 'content');
assert.equal(integrationName, 'live fast die young');
});
it('Queries for Admin API key when such is provided', async function () {
const ApiKeyModel = {
findOne: sinon
.stub()
.withArgs({
id: 'key_id'
}, {
withRelated: ['integration']
})
.resolves({
relations: {
integration: {
get: () => 'Tri Hita Karana'
}
}
})
};
const versionNotificationsDataService = new VersionNotificationsDataService({
UserModel: {},
ApiKeyModel,
settingsService: {}
});
const integrationName = await versionNotificationsDataService.getIntegrationName('key_id', 'admin');
assert.equal(integrationName, 'Tri Hita Karana');
});
});
}); });