|
| 1 | +const configurable_auth = require("../middleware/configurable_auth"); |
| 2 | +const { Context } = require("../util/context"); |
| 3 | +const { Endpoint } = require("../util/expressutil"); |
| 4 | +const BaseService = require("./BaseService"); |
| 5 | +const { Interface } = require("./drivers/meta/Construct"); |
| 6 | + |
| 7 | +const PERM_SEE_ALL = 'kernel-info:see-all-services'; |
| 8 | +const PERM_SEE_DRIVERS = 'kernel-info:see-all-drivers'; |
| 9 | + |
| 10 | +class KernelInfoService extends BaseService { |
| 11 | + async _init () { |
| 12 | + // |
| 13 | + } |
| 14 | + |
| 15 | + ['__on_install.routes'] (_, { app }) { |
| 16 | + const router = (() => { |
| 17 | + const require = this.require; |
| 18 | + const express = require('express'); |
| 19 | + return express.Router(); |
| 20 | + })(); |
| 21 | + |
| 22 | + app.use('/', router); |
| 23 | + |
| 24 | + Endpoint({ |
| 25 | + route: '/lsmod', |
| 26 | + methods: ['GET', 'POST'], |
| 27 | + mw: [ |
| 28 | + configurable_auth(), |
| 29 | + ], |
| 30 | + handler: async (req, res) => { |
| 31 | + const svc_permission = this.services.get('permission'); |
| 32 | + |
| 33 | + const actor = Context.get('actor'); |
| 34 | + const can_see_all = actor && |
| 35 | + await svc_permission.check(actor, PERM_SEE_ALL); |
| 36 | + const can_see_drivers = actor && |
| 37 | + await svc_permission.check(actor, PERM_SEE_DRIVERS); |
| 38 | + |
| 39 | + const interfaces = {}; |
| 40 | + const svc_registry = this.services.get('registry'); |
| 41 | + const col_interfaces = svc_registry.get('interfaces'); |
| 42 | + for ( const interface_name of col_interfaces.keys() ) { |
| 43 | + const iface = col_interfaces.get(interface_name); |
| 44 | + console.log('-->', interface_name, iface); |
| 45 | + if ( iface === undefined ) continue; |
| 46 | + if ( iface.no_sdk ) continue; |
| 47 | + interfaces[interface_name] = { |
| 48 | + spec: (new Interface( |
| 49 | + iface, |
| 50 | + { name: interface_name } |
| 51 | + )).serialize(), |
| 52 | + implementors: {} |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + const services = []; |
| 57 | + const modules = []; |
| 58 | + for ( const k in this.services.modules_ ) { |
| 59 | + const module_info = { |
| 60 | + name: k, |
| 61 | + services: [] |
| 62 | + }; |
| 63 | + |
| 64 | + modules.push(module_info); |
| 65 | + |
| 66 | + for ( const s_k of this.services.modules_[k].services_l ) { |
| 67 | + const service_info = { |
| 68 | + name: s_k, |
| 69 | + traits: [] |
| 70 | + }; |
| 71 | + services.push(service_info); |
| 72 | + |
| 73 | + const service = this.services.get(s_k); |
| 74 | + if ( service.list_traits ) { |
| 75 | + const traits = service.list_traits(); |
| 76 | + for ( const trait of traits ) { |
| 77 | + const corresponding_iface = interfaces[trait]; |
| 78 | + if ( ! corresponding_iface ) continue; |
| 79 | + corresponding_iface.implementors[s_k] = {}; |
| 80 | + } |
| 81 | + service_info.traits = service.list_traits(); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // If actor doesn't have permission to see all drivers, |
| 87 | + // (granted by either "can_see_all" or "can_see_drivers") |
| 88 | + if ( ! can_see_all && ! can_see_drivers ) { |
| 89 | + // only show interfaces with at least one implementation |
| 90 | + // that the actor has permission to use |
| 91 | + for ( const iface_name in interfaces ) { |
| 92 | + for ( const impl_name in interfaces[iface_name].implementors ) { |
| 93 | + const perm = `service:${impl_name}:ii:${iface_name}`; |
| 94 | + const can_see_this = actor && |
| 95 | + await svc_permission.check(actor, perm); |
| 96 | + if ( ! can_see_this ) { |
| 97 | + delete interfaces[iface_name].implementors[impl_name]; |
| 98 | + } |
| 99 | + } |
| 100 | + if ( Object.keys(interfaces[iface_name].implementors).length < 1 ) { |
| 101 | + delete interfaces[iface_name]; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + res.json({ |
| 107 | + interfaces, |
| 108 | + ...(can_see_all ? { services } : {}) |
| 109 | + }); |
| 110 | + } |
| 111 | + }).attach(router); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +module.exports = { |
| 116 | + KernelInfoService, |
| 117 | +}; |
0 commit comments