Skip to content

Commit b867fb1

Browse files
committed
refactor: set strictnullchecks = true
1 parent ac3a957 commit b867fb1

File tree

115 files changed

+1761
-1191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+1761
-1191
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@typescript-eslint/consistent-type-assertions": 0,
3939
"@typescript-eslint/no-dynamic-delete": 0,
4040
"@typescript-eslint/prefer-nullish-coalescing": 0,
41-
"@typescript-eslint/unbound-method": 0
41+
"@typescript-eslint/unbound-method": 0,
42+
"@typescript-eslint/no-non-null-assertion": "off"
4243
}
4344
}

src/DataProcessor.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { Maintenance, type MaintenanceEvent } from './stateMachines/maintenance/
1616
import { Activation, type ActivationEvent } from './stateMachines/activation.js'
1717
import ClientResponseMsg from './utils/ClientResponseMsg.js'
1818
import { parseChunkedMessage } from './utils/parseChunkedMessage.js'
19-
import { UNEXPECTED_PARSE_ERROR } from './utils/constants.js'
19+
import { UNEXPECTED_PARSE_ERROR, type UnexpectedParseError } from './utils/constants.js'
2020
import { SyncTimeEventType } from './stateMachines/maintenance/syncTime.js'
2121
import { ChangePasswordEventType } from './stateMachines/maintenance/changePassword.js'
2222
import { SyncHostNameEventType } from './stateMachines/maintenance/syncHostName.js'
@@ -40,7 +40,7 @@ export class DataProcessor {
4040
*/
4141
async processData (message: WebSocket.Data, clientId: string): Promise<ClientMsg | null> {
4242
try {
43-
let clientMsg: ClientMsg = null
43+
let clientMsg: ClientMsg
4444

4545
try {
4646
clientMsg = this.validator.parseClientMsg(message, clientId)
@@ -77,6 +77,7 @@ export class DataProcessor {
7777
ClientResponseMsg.get(clientId, null, 'error', 'failed', 'request failed')
7878
}
7979
}
80+
return null
8081
}
8182

8283
async activateDevice (clientMsg: ClientMsg, clientId: string, activation: Activation = new Activation()): Promise<void> {
@@ -104,7 +105,7 @@ export class DataProcessor {
104105
async handleResponse (clientMsg: ClientMsg, clientId: string): Promise<void> {
105106
const clientObj = devices[clientId]
106107
let resolveValue = null
107-
let rejectValue = null
108+
let rejectValue: UnexpectedParseError | HttpZResponseModel | null = null
108109
let statusCode = -1
109110
try {
110111
const { parse } = pkg
@@ -124,10 +125,12 @@ export class DataProcessor {
124125
rejectValue = new UNEXPECTED_PARSE_ERROR()
125126
}
126127
if (clientObj.pendingPromise != null) {
127-
if (resolveValue) {
128-
clientObj.resolve(resolveValue)
129-
} else {
130-
clientObj.reject(rejectValue)
128+
if (clientObj.resolve && clientObj.reject) {
129+
if (resolveValue) {
130+
clientObj.resolve(resolveValue)
131+
} else {
132+
clientObj.reject(rejectValue)
133+
}
131134
}
132135
}
133136
this.logger.debug(`Device ${clientId}` +
@@ -177,7 +180,7 @@ export class DataProcessor {
177180
return mEvent
178181
}
179182

180-
setConnectionParams (clientId: string, username: string = null, password: string = null, uuid: string = null): void {
183+
setConnectionParams (clientId: string, username: string | null = null, password: string | null = null, uuid: string | null = null): void {
181184
const clientObj = devices[clientId]
182185
clientObj.connectionParams = {
183186
port: 16992,

src/DomainCredentialManager.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import { type ISecretManagerService, type CertCredentials } from './interfaces/I
1212
export class DomainCredentialManager implements IDomainCredentialManager {
1313
private readonly amtDomains: IDomainsTable
1414
private readonly logger: ILogger
15-
private readonly secretsManager: ISecretManagerService = null
15+
private readonly secretsManager: ISecretManagerService | null = null
1616

1717
constructor (logger: ILogger, amtDomains: IDomainsTable, secretsManager?: ISecretManagerService) {
1818
this.amtDomains = amtDomains
1919
this.logger = logger
20-
this.secretsManager = secretsManager
20+
this.secretsManager = secretsManager ?? null
2121
}
2222

2323
/**
@@ -26,7 +26,7 @@ export class DomainCredentialManager implements IDomainCredentialManager {
2626
* @param {string} tenantId
2727
* @returns {AMTDomain} returns domain object
2828
*/
29-
async getProvisioningCert (domainSuffix: string, tenantId: string): Promise<AMTDomain> {
29+
async getProvisioningCert (domainSuffix: string, tenantId: string): Promise<AMTDomain | null> {
3030
const domain = await this.amtDomains.getDomainByDomainSuffix(domainSuffix, tenantId)
3131
this.logger.debug(`domain : ${JSON.stringify(domain)}`)
3232

src/HttpHandler.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ it('should return a null when no xml is passed to wrap a WSMan request', async (
122122
username: 'admin',
123123
password: 'P@ssw0rd'
124124
}
125-
const result = httpHandler.wrapIt(null, connectionParams)
125+
const result = httpHandler.wrapIt(null as any, connectionParams)
126126
expect(result).toBe(null)
127127
})

src/HttpHandler.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class HttpHandler {
2424
this.logger = new Logger('HttpHandler')
2525
}
2626

27-
wrapIt (data: string, connectionParams: connectionParams): string {
27+
wrapIt (data: string, connectionParams: connectionParams): string | null {
2828
try {
2929
const url = '/wsman'
3030
const action = 'POST'
@@ -34,7 +34,7 @@ export class HttpHandler {
3434
}
3535
if (connectionParams.digestChallenge != null) {
3636
// Prepare an Authorization request header from the 401 unauthorized response from AMT
37-
let responseDigest = null
37+
let responseDigest: string | null = null
3838
// console nonce should be a unique opaque quoted string
3939
connectionParams.consoleNonce = Math.random().toString(36).substring(7)
4040
const nc = ('00000000' + (this.nonceCounter++).toString(16)).slice(-8)
@@ -77,7 +77,7 @@ export class HttpHandler {
7777

7878
// Prepares Authorization Request Header
7979
digestIt (params: object): string {
80-
const paramNames = []
80+
const paramNames: string[] = []
8181
for (const i in params) {
8282
paramNames.push(i)
8383
}

src/Index.ts

+5-23
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import { WebSocketListener } from './WebSocketListener.js'
1010
import { Configurator } from './Configurator.js'
1111
import { Environment } from './utils/Environment.js'
1212
import { type RPSConfig } from './models/index.js'
13-
import { parseValue } from './utils/parseEnvValue.js'
1413
import routes from './routes/index.js'
15-
import rc from 'rc'
1614
import { MqttProvider } from './utils/MqttProvider.js'
1715
import { DbCreatorFactory } from './factories/DbCreatorFactory.js'
1816
import { backOff } from 'exponential-backoff'
@@ -30,22 +28,6 @@ import { URL, fileURLToPath, pathToFileURL } from 'node:url'
3028
const __dirname = fileURLToPath(new URL('.', import.meta.url))
3129
const log = new Logger('Index')
3230

33-
// To merge ENV variables. consider after lowercasing ENV since our config keys are lowercase
34-
process.env = Object.keys(process.env)
35-
.reduce((destination, key) => {
36-
destination[key.toLowerCase()] = parseValue(process.env[key])
37-
return destination
38-
}, {})
39-
40-
// build config object
41-
const config: RPSConfig = rc('rps')
42-
config.delay_activation_sync = config.delay_timer * 1000
43-
config.delay_setup_and_config_sync = 5000
44-
config.delay_tls_put_data_sync = 5000
45-
log.silly(`config: ${JSON.stringify(config, null, 2)}`)
46-
47-
Environment.Config = config
48-
4931
const app = express()
5032
app.use(cors())
5133
app.use(express.urlencoded())
@@ -75,7 +57,7 @@ export const startItUp = (): void => {
7557
const serverForEnterpriseAssistant: WSEnterpriseAssistantListener = new WSEnterpriseAssistantListener(new Logger('WSEnterpriseAssistantListener'))
7658
const server: WebSocketListener = new WebSocketListener(new Logger('WebSocketListener'), configurator.dataProcessor)
7759

78-
const mqtt: MqttProvider = new MqttProvider(config)
60+
const mqtt: MqttProvider = new MqttProvider(Environment.Config)
7961
mqtt.connectBroker()
8062

8163
const dbFactory = new DbCreatorFactory()
@@ -125,8 +107,8 @@ export const startItUp = (): void => {
125107
await waitForSecretsManager(configurator.secretsManager)
126108
})
127109
.then(() => {
128-
app.listen(config.web_port, () => {
129-
log.info(`RPS Microservice Rest APIs listening on http://:${config.web_port}.`)
110+
app.listen(Environment.Config.web_port, () => {
111+
log.info(`RPS Microservice Rest APIs listening on http://:${Environment.Config.web_port}.`)
130112
})
131113
server.connect()
132114
serverForEnterpriseAssistant.connect()
@@ -144,8 +126,8 @@ export async function setupServiceManager (config: RPSConfig): Promise<void> {
144126
await processServiceConfigs(consul, config)
145127
}
146128

147-
if (config.consul_enabled) {
148-
setupServiceManager(config).then(() => {
129+
if (Environment.Config.consul_enabled) {
130+
setupServiceManager(Environment.Config).then(() => {
149131
startItUp()
150132
}).catch(err => {
151133
log.error(`Unable to reach consul: ${err}`)

src/NodeForge.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class NodeForge {
1818
}
1919

2020
getBags (pkcs12Pfx: forge.pkcs12.Pkcs12Pfx, filter: forge.pkcs12.BagsFilter): {
21-
[key: string]: forge.pkcs12.Bag[]
21+
[key: string]: forge.pkcs12.Bag[] | undefined
2222
localKeyId?: forge.pkcs12.Bag[]
2323
friendlyName?: forge.pkcs12.Bag[]
2424
} {

src/ProfileManager.ts

+37-25
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ export class ProfileManager implements IProfileManager {
2929
* @param {string} profileName profile to look up
3030
* @returns {string} returns the activation to be performed
3131
*/
32-
public async getActivationMode (profileName: string, tenantId: string): Promise<string> {
32+
public async getActivationMode (profileName: string, tenantId: string): Promise<string | null> {
3333
const profile = await this.getAmtProfile(profileName, tenantId)
34-
let activation: string
34+
let activation: string | null = null
3535

3636
if (profile?.activation) {
3737
this.logger.debug(`found activation for profile ${profileName}`)
@@ -48,15 +48,19 @@ export class ProfileManager implements IProfileManager {
4848
* @param {string} profile of cira config
4949
* @returns {string} returns the config for CIRA for a given profile
5050
*/
51-
public async getCiraConfiguration (profileName: string, tenantId: string): Promise<CIRAConfig> {
51+
public async getCiraConfiguration (profileName: string | null, tenantId: string): Promise<CIRAConfig | null> {
5252
const profile = await this.getAmtProfile(profileName, tenantId)
53-
let ciraConfig: CIRAConfig
53+
let ciraConfig: CIRAConfig | null = null
5454

55-
if (profile?.ciraConfigName && profile.ciraConfigObject) {
56-
this.logger.debug(`found CIRAConfigObject for profile: ${profile.profileName}`)
57-
ciraConfig = profile.ciraConfigObject
55+
if (profile) {
56+
if (profile.ciraConfigName && profile.ciraConfigObject) {
57+
this.logger.debug(`found CIRAConfigObject for profile: ${profile.profileName}`)
58+
ciraConfig = profile.ciraConfigObject
59+
} else {
60+
this.logger.debug(`unable to find CIRAConfig for profile ${profile.profileName}`)
61+
}
5862
} else {
59-
this.logger.debug(`unable to find CIRAConfig for profile ${profile.profileName}`)
63+
this.logger.debug(`unable to find CIRAConfig for profile ${profileName}`)
6064
}
6165

6266
return ciraConfig
@@ -67,9 +71,9 @@ export class ProfileManager implements IProfileManager {
6771
* @param {string} profileName profile name of amt password
6872
* @returns {string} returns the amt password for a given profile
6973
*/
70-
public async getAmtPassword (profileName: string, tenantId: string): Promise<string> {
71-
const profile: AMTConfiguration = await this.getAmtProfile(profileName, tenantId)
72-
let amtPassword: string
74+
public async getAmtPassword (profileName: string, tenantId: string): Promise<string | null> {
75+
const profile: AMTConfiguration | null = await this.getAmtProfile(profileName, tenantId)
76+
let amtPassword: string | null = null
7377
if (profile) {
7478
if (profile.generateRandomPassword) {
7579
amtPassword = PasswordHelper.generateRandomPassword()
@@ -82,27 +86,30 @@ export class ProfileManager implements IProfileManager {
8286
} else if (this.secretsManager) {
8387
amtPassword = await this.secretsManager.getSecretFromKey(`profiles/${profileName}`, 'AMT_PASSWORD')
8488
} else {
85-
amtPassword = profile.amtPassword
89+
if (profile.amtPassword) {
90+
amtPassword = profile.amtPassword
91+
}
8692
}
87-
this.logger.debug(`found amtPassword for profile ${profileName}`)
8893
if (!amtPassword) {
8994
this.logger.error('password cannot be blank')
9095
throw new Error('password cannot be blank')
9196
}
97+
this.logger.debug(`found amtPassword for profile ${profileName}`)
9298
return amtPassword
9399
} else {
94100
this.logger.error(`unable to find amtPassword for profile ${profileName}`)
95101
}
102+
return null
96103
}
97104

98105
/**
99106
* @description Retrieves the amt password set in the configuration or generates a nonstatic password
100107
* @param {string} profileName profile name of amt password
101108
* @returns {string} returns the amt password for a given profile
102109
*/
103-
public async getMEBxPassword (profileName: string, tenantId: string): Promise<string> {
104-
const profile: AMTConfiguration = await this.getAmtProfile(profileName, tenantId)
105-
let mebxPassword: string
110+
public async getMEBxPassword (profileName: string, tenantId: string): Promise<string | null> {
111+
const profile: AMTConfiguration | null = await this.getAmtProfile(profileName, tenantId)
112+
let mebxPassword: string | null = null
106113
if (profile) {
107114
if (profile.generateRandomMEBxPassword) {
108115
mebxPassword = PasswordHelper.generateRandomPassword()
@@ -115,18 +122,20 @@ export class ProfileManager implements IProfileManager {
115122
} else if (this.secretsManager) {
116123
mebxPassword = await this.secretsManager.getSecretFromKey(`profiles/${profileName}`, 'MEBX_PASSWORD')
117124
} else {
118-
mebxPassword = profile.mebxPassword
125+
if (profile.mebxPassword) {
126+
mebxPassword = profile.mebxPassword
127+
}
119128
}
120-
121-
this.logger.debug(`found amtPassword for profile ${profileName}`)
122129
if (!mebxPassword) {
123130
this.logger.error('mebx password cannot be blank')
124131
throw new Error('mebx password cannot be blank')
125132
}
133+
this.logger.debug(`found amtPassword for profile ${profileName}`)
126134
return mebxPassword
127135
} else {
128136
this.logger.error(`unable to find mebxPassword for profile ${profileName}`)
129137
}
138+
return null
130139
}
131140

132141
/**
@@ -136,8 +145,8 @@ export class ProfileManager implements IProfileManager {
136145
* @returns {string} returns the MPS password for a given profile
137146
*/
138147
public async getMPSPassword (profileName: string, tenantId: string): Promise<string> {
139-
const profile: AMTConfiguration = await this.getAmtProfile(profileName, tenantId)
140-
let mpsPassword: string
148+
const profile: AMTConfiguration | null = await this.getAmtProfile(profileName, tenantId)
149+
let mpsPassword: string | null = null
141150

142151
if (profile?.ciraConfigObject) {
143152
mpsPassword = PasswordHelper.generateRandomPassword()
@@ -164,14 +173,17 @@ export class ProfileManager implements IProfileManager {
164173
* @param {string} profile
165174
* @returns {AMTConfiguration} returns AMTConfig object if profile exists otherwise null.
166175
*/
167-
public async getAmtProfile (profile: string, tenantId: string): Promise<AMTConfiguration> {
176+
public async getAmtProfile (profile: string | null, tenantId: string): Promise<AMTConfiguration | null> {
168177
try {
169178
if (!profile) {
170179
return null
171180
}
172-
const amtProfile: AMTConfiguration = await this.amtConfigurations.getByName(profile, tenantId)
181+
const amtProfile: AMTConfiguration | null = await this.amtConfigurations.getByName(profile, tenantId)
182+
if (!amtProfile) {
183+
return null
184+
}
173185
// If the CIRA Config associated with profile, retrieves from DB
174-
if (amtProfile?.ciraConfigName != null) {
186+
if (amtProfile.ciraConfigName != null) {
175187
amtProfile.ciraConfigObject = await this.amtConfigurations.getCiraConfigForProfile(amtProfile.ciraConfigName, tenantId)
176188
}
177189
// If the TLS Config associated with profile, retrieves from DB
@@ -182,7 +194,7 @@ export class ProfileManager implements IProfileManager {
182194
}
183195
}
184196
// If the CIRA Config associated with profile, retrieves from DB
185-
if (amtProfile?.ieee8021xProfileName != null) {
197+
if (amtProfile.ieee8021xProfileName != null) {
186198
amtProfile.ieee8021xProfileObject = await this.amtConfigurations.get8021XConfigForProfile(amtProfile.ieee8021xProfileName, tenantId)
187199
}
188200
this.logger.debug(`AMT Profile returned from db: ${amtProfile?.profileName}`)

0 commit comments

Comments
 (0)