|
| 1 | +import { WirelessConfig } from 'models/RCS.Config.js' |
| 2 | +import Logger from '../../../Logger.js' |
| 3 | +import { NOT_FOUND_EXCEPTION, NOT_FOUND_MESSAGE } from '../../../utils/constants.js' |
| 4 | +import handleError from '../../../utils/handleError.js' |
| 5 | +import { RPSError } from '../../../utils/RPSError.js' |
| 6 | +import { type Request, type Response } from 'express' |
| 7 | +import yaml from 'js-yaml' |
| 8 | +import { encryptWithRandomKey } from '../../../utils/encryptWithRandomKey.js' |
| 9 | + |
| 10 | +export async function exportProfile(req: Request, res: Response): Promise<void> { |
| 11 | + const log = new Logger('exportProfile') |
| 12 | + const { profileName, domainName } = req.params |
| 13 | + try { |
| 14 | + const result = await req.db.profiles.getByName(profileName, req.tenantId) |
| 15 | + if (result == null) { |
| 16 | + throw new RPSError(NOT_FOUND_MESSAGE('AMT', profileName), NOT_FOUND_EXCEPTION) |
| 17 | + } |
| 18 | + if (result.ciraConfigName && result.ciraConfigName !== '') { |
| 19 | + result.ciraConfigObject = await req.db.ciraConfigs.getByName(result.ciraConfigName, req.tenantId) |
| 20 | + if (result.ciraConfigObject == null) { |
| 21 | + throw new RPSError(NOT_FOUND_MESSAGE('CIRA', result.ciraConfigName), NOT_FOUND_EXCEPTION) |
| 22 | + } |
| 23 | + } |
| 24 | + if (result.activation === 'acmactivate') { |
| 25 | + const domainStuff = await req.db.domains.getByName(domainName, req.tenantId) |
| 26 | + ;(result as any).domainObject = domainStuff |
| 27 | + } |
| 28 | + const wifiConfigs: any[] = [] |
| 29 | + if (result.wifiConfigs && result.wifiConfigs.length > 0) { |
| 30 | + for (const wifiConfig of result.wifiConfigs) { |
| 31 | + const wifiConfigObject = await req.db.wirelessProfiles.getByName(wifiConfig.profileName, req.tenantId) |
| 32 | + if (wifiConfigObject == null) { |
| 33 | + throw new RPSError(NOT_FOUND_MESSAGE('Wireless', wifiConfig.profileName), NOT_FOUND_EXCEPTION) |
| 34 | + } |
| 35 | + wifiConfigs.push({ |
| 36 | + profileName: wifiConfigObject.profileName, |
| 37 | + ssid: wifiConfigObject.ssid, |
| 38 | + priority: wifiConfig.priority, |
| 39 | + authenticationMethod: wifiConfigObject.authenticationMethod, |
| 40 | + encryptionMethod: wifiConfigObject.encryptionMethod, |
| 41 | + pskPassphrase: wifiConfigObject.pskPassphrase, |
| 42 | + ieee8021xProfileName: wifiConfigObject.ieee8021xProfileName |
| 43 | + }) |
| 44 | + } |
| 45 | + } |
| 46 | + let ieee8021xConfigs: any[] = [] |
| 47 | + if (result.ieee8021xProfileName && result.ieee8021xProfileName !== '') { |
| 48 | + const ieee8021xProfile = await req.db.ieee8021xProfiles.getByName(result.ieee8021xProfileName, req.tenantId) |
| 49 | + if (ieee8021xProfile == null) { |
| 50 | + throw new RPSError(NOT_FOUND_MESSAGE('802.1x', result.ieee8021xProfileName), NOT_FOUND_EXCEPTION) |
| 51 | + } |
| 52 | + ieee8021xConfigs = [ |
| 53 | + { |
| 54 | + profileName: ieee8021xProfile.profileName, |
| 55 | + username: ieee8021xProfile.username, |
| 56 | + password: ieee8021xProfile.password, |
| 57 | + authenticationProtocol: ieee8021xProfile.authenticationProtocol |
| 58 | + // clientCert: ieee8021xProfile.clientCert, |
| 59 | + // caCert: ieee8021xProfile.caCert, |
| 60 | + // privateKey: ieee8021xProfile.privateKey |
| 61 | + } |
| 62 | + ] |
| 63 | + } |
| 64 | + |
| 65 | + // Map to Go struct shape |
| 66 | + const output = { |
| 67 | + password: result.amtPassword ?? '', // Adjust as needed |
| 68 | + tlsConfig: { |
| 69 | + //delay: result.tlsDelay ?? 3, |
| 70 | + mode: result.tlsMode ?? '' |
| 71 | + }, |
| 72 | + wiredConfig: { |
| 73 | + dhcp: result.dhcpEnabled ?? true, |
| 74 | + static: !result.dhcpEnabled, |
| 75 | + ipsync: result.ipSyncEnabled ?? false, |
| 76 | + // ipaddress: result.ipAddress ?? '', |
| 77 | + // subnetmask: result.subnetMask ?? '', |
| 78 | + // gateway: result.gateway ?? '', |
| 79 | + // primarydns: result.primaryDns ?? '', |
| 80 | + // secondarydns: result.secondaryDns ?? '', |
| 81 | + ieee8021xProfileName: result.ieee8021xProfileName ?? '' |
| 82 | + }, |
| 83 | + wifiConfigs, |
| 84 | + wifiSyncEnabled: result.localWifiSyncEnabled ?? false, |
| 85 | + ieee8021xConfigs, |
| 86 | + acmactivate: |
| 87 | + result.activation === 'acmactivate' |
| 88 | + ? { |
| 89 | + amtPassword: result.amtPassword ?? '', |
| 90 | + provisioningCert: '', |
| 91 | + provisioningCertPwd: '' |
| 92 | + } |
| 93 | + : null, |
| 94 | + enterpriseAssistant: { |
| 95 | + eaAddress: '', //result.eaAddress ?? '', |
| 96 | + eaUsername: '', //result.eaUsername ?? '', |
| 97 | + eaPassword: '', //result.eaPassword ?? '', |
| 98 | + eaConfigured: '' //result.eaConfigured ?? false |
| 99 | + }, |
| 100 | + // stopConfig: result.stopConfig ?? false, |
| 101 | + ccmactivate: |
| 102 | + result.activation === 'ccmactivate' |
| 103 | + ? { |
| 104 | + amtPassword: result.amtPassword ?? '' |
| 105 | + } |
| 106 | + : null |
| 107 | + } |
| 108 | + |
| 109 | + const yamlStr = yaml.dump(output) |
| 110 | + // Encrypt the YAML string and provide the key |
| 111 | + const { cipherText, key } = encryptWithRandomKey(yamlStr) |
| 112 | + res.status(200).send({ |
| 113 | + filename: `${profileName}.yaml.enc`, |
| 114 | + content: cipherText, |
| 115 | + key |
| 116 | + }) |
| 117 | + } catch (error) { |
| 118 | + handleError(log, profileName, req, res, error) |
| 119 | + } |
| 120 | +} |
0 commit comments